package com.seed.uaa.web.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seed.core.annotation.Declares;
import com.seed.core.annotation.EntityClass;
import com.seed.core.pojo.R;
import com.seed.core.pojo.SetDTO;
import com.seed.core.pojo.StatPage;
import com.seed.core.annotation.Searches;
import com.seed.core.annotation.SeedDeclare;
import com.seed.uaa.pojo.OwnerAction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理")
@RestController
@RequestMapping("user")
@EntityClass(User.class)
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "分页列表")
    @GetMapping("page")
    @Declares("id,organization.name,name,avatar,username,expire_date,usable,loginable,enable,active")
    public R<StatPage<User>> page(Integer pageNumber, Integer pageSize) {
        return R.data(userService.page(pageNumber, pageSize));
    }

    @Operation(summary = "保存")
    @PostMapping("save")
    @Declares("organization_id, name, avatar")
    public R<Boolean> save(@RequestBody User user) {
        return R.data(userService.save(user));
    }

    @Operation(summary = "设置名称")
    @GetMapping("name/{id}")
    public R<?> name(@PathVariable String id) {
        return R.data(userService.get(id));
    }

    @Operation(summary = "设置名称")
    @PutMapping("name")
    public R<Boolean> name(@RequestBody SetDTO dto) {
        return R.data(userService.set(dto));
    }

    @Operation(summary = "设置头像")
    @GetMapping("avatar/{id}")
    public R<?> avatar(@PathVariable String id) {
        return R.data(userService.get(id));
    }

    @Operation(summary = "设置头像")@SaCheckPermission
    @PutMapping("avatar")
    public R<Boolean> avatar(@RequestBody SetDTO dto) {
        return R.data(userService.set(dto));
    }

    @Operation(summary = "更新")
    @GetMapping("update/{id}")
    @Declares("id, organization_id, name, avatar")
    public R<User> update(@PathVariable String id) {
        return R.data(userService.detail(id));
    }

    @Operation(summary = "更新")
    @PutMapping("update")
    @Declares("id, organization_id, name, avatar")
    public R<Boolean> update(@RequestBody User user) {
        return R.data(userService.update(user));
    }

    @GetMapping("username/{id}")
    @Operation(summary = "设置账号")
    public R<?> username(@PathVariable String id) {
        return R.data(userService.get(id));
    }

    @PutMapping("username")
    @Operation(summary = "设置账号")
    public R<Boolean> username(@RequestBody SetDTO dto) {
        return R.data(userService.set(dto));
    }

    @Operation(summary = "重置密码")
    @PutMapping("reset")
    public R<Boolean> reset(String id) {
        return R.data(userService.resetPassword(id));
    }

    @Operation(summary = "设置截止日期")
    @GetMapping("expireDate/{id}")
    @SeedDeclare("expireDate")
    public R<?> expireDate(@PathVariable String id) {
        return R.data(userService.get(id));
    }

    @Operation(summary = "设置截止日期")
    @PutMapping("expireDate")
    public R<Boolean> expireDate(@RequestBody SetDTO dto) {
        return R.data(userService.set(dto));
    }

    @Operation(summary = "设置启用")
    @GetMapping("usable/{id}")
    @Declares("id, usable, loginable, enable, reason")
    public R<User> usable(@PathVariable String id) {
        return R.data(userService.detail(id));
    }

    @Operation(summary = "设置启用")
    @PutMapping("usable")
    @Declares("usable, loginable, enable, reason")
    public R<Boolean> usable(@RequestBody User user) {
        return R.data(userService.update(user));
    }

    @Operation(summary = "激活账户")
    @GetMapping("active/{id}")
    @SeedDeclare(declare = "active", value = "yes")
    public R<?> active(@PathVariable String id) {
        return R.data(userService.get(id));
    }

    @Operation(summary = "激活账户")
    @PutMapping("active")
    @SeedDeclare(declare = "active", value = "yes")
    public R<Boolean> active(@RequestBody SetDTO dto) {
        return R.data(userService.set(dto));
    }

    @Operation(summary = "获取角色")
    @GetMapping("roles/{id}")
    @SeedDeclare("roles")
    public R<Object> roles(@PathVariable String id) {
        return R.data(userService.get(id));
    }

    @Operation(summary = "分配角色")
    @PutMapping("roles")
    @SeedDeclare("roles")
    public R<Boolean> roles(@RequestBody SetDTO dto) {
        return R.data(userService.set(dto));
    }

    @Operation(summary = "查看功能权限")
    @GetMapping("getActions/{id}")
    public R<OwnerAction> getActions(@PathVariable String id) {
        return R.data(userService.getUserAction(id));
    }

    @Operation(summary = "获取功能权限")
    @GetMapping("actions/{id}")
    public R<List<String>> actions(@PathVariable String id) {
        return R.data(userService.getActions(id));
    }

    @Operation(summary = "分配功能权限")
    @PutMapping("actions")
    public R<Boolean> actions(@RequestBody SetDTO dto) {
        return R.data(userService.setActions(dto));
    }

    @Operation(summary = "获取数据权限")
    @GetMapping("datas/{id}")
    public R<List<String>> datas(@PathVariable String id, String actionPath) {
        return R.data(userService.getDatas(id, actionPath));
    }

    @Operation(summary = "分配数据权限")
    @PutMapping("datas")
    public R<Boolean> datas(String id, String actionPath, @RequestBody List<String> params) {
        return R.data(userService.setDatas(id, actionPath, params));
    }
    @Operation(summary = "获取字段权限")
    @GetMapping("fields/{id}")
    public R<Map<String, String>> fields(@PathVariable String id, String actionPath) {
        return R.data(userService.getFields(id, actionPath));
    }

    @Operation(summary = "分配字段权限")
    @PutMapping("fields")
    public R<Boolean> fields(String id, String actionPath, @RequestBody Map<String, String> params) {
        return R.data(userService.setFields(id, actionPath, params));
    }

    @Operation(summary = "查找用户")
    @GetMapping("lookup")
    @Declares("id,organization.name,name,avatar,username")
    @Searches("organization.name_like,name_like,username_like")
    public R<StatPage<User>> lookup(Integer pageNumber, Integer pageSize) {
        return R.data(userService.page(pageNumber, pageSize));
    }

    @Operation(summary = "详情")
    @GetMapping("detail/{id}")
    public R<User> detail(@PathVariable String id) {
        return R.data(userService.detail(id));
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{id}")
    public R<Boolean> remove(@PathVariable String id) {
        return R.data(userService.remove(id));
    }
}
